<#assign tag = fn.tag>
<#if execute?? && execute.allCategories??>
<#assign tabId=tabId!fn.id>
<style>
	.privilegelist {
		float: left;
		min-width: 600px;
		max-width: 800px;
	}
	.privilegelist td {
		padding: 4px;
	}
	.privilegelist.tablelist .th {
		padding-left: 5px;
	}
</style>
<script>
	<#assign imagePathPrefix = base + '/static/espirit'>
	function switchFlag(imgObj, id) {
		var $imgFlag = $(imgObj);
		var $privilege = $('#' + id);
		var flags = $imgFlag.attr('flag');
		var flagArray = flags.split(',');
		var privilege = $imgFlag.attr('privilege');
		var privilegeArray = privilege.split(',');
		var curValue = $privilege.val();
		var index = 0;
		if (curValue != '') {
			for (var i = 0; i < privilegeArray.length; i++) {
				if (curValue == privilegeArray[i]) {
					index = i;
					break;
				}
			}
		}
		var nextIndex = index + 1;
		if (nextIndex == flagArray.length) {
			nextIndex = 0;
		}
		$privilege.val(privilegeArray[nextIndex]);
		$imgFlag.attr('src', '${imagePathPrefix}/images/permission/flag' + flagArray[nextIndex] + '.gif');
	}
	function initPrivilege(tabId, privileges) {
		$('#tabs_' + tabId).find('img').attr('src', '${imagePathPrefix}/images/permission/flag0.gif');
		$('#tabs_' + tabId).find('input[name=privileges]').val('');
		for (var i = 0; i < privileges.length; i++) {
			var privilege = privileges[i];
			var index = privilege.indexOf(':');
			var imgId = privilege.substring(0, index);
			var flag = privilege.substring(index + 1);
			var $imgFlag = $('#' + tabId + imgId);
			$imgFlag.attr('src', '${imagePathPrefix}/images/permission/flag' + flag + '.gif');
			var flags = $imgFlag.attr('flag');
			if (!flags) {
				continue;
			}
			var flagArray = flags.split(',');
			var privilege = $imgFlag.attr('privilege');
			var privilegeArray = privilege.split(',');
			var index = 0;
			for (; index < flagArray.length; index++) {
				if (flagArray[index] == flag) {
					break;
				}
			}
			$imgFlag.next().val(privilegeArray[index]);
		}
	}
	$(document).ready(function(){
		$("#tabs_${tabId}").tabs().show();
		
		var privileges = ${fn.json(selectedPrivileges)};
		if (privileges && privileges.length > 0) {
			setTimeout(function() {
				initPrivilege('${tabId}', privileges);
			}, 1);
		}
	});
</script>

<div id="tabs_${tabId}" style="display: none;border-style: none">
	<ul class="tabHeader">
	
	<#list execute.allCategories as category>
		<li><a href="#tabs_${tabId}_${category}">${action.getText('module.' + category)}</a></li>
	</#list>
	</ul>

	<#assign privilegeIndex=0>
	<#list execute.allCategories as category>
		<div id="tabs_${tabId}_${category}" class="standardPermission">
			<table id="icItemInputTable" class="tablelist fullWidth privilegelist">
				<thead>
					<tr>
						<th><div class="th">${action.getText('业务实体')}</div></th>
						<#list execute.methods as method>
							<th><div class="th">${action.getText('action.' + method)}</div></th>
						</#list>
					</tr>
				</thead>
				
				<tbody>
					<#list execute.getModels(category) as modelName>
						<tr>
							<td>${execute.getModelI18n(modelName)}</td>
							<#list execute.methods as methodName>
								<td>
									<#assign privilegeFlags=execute.getPrivilegeFlags(category, modelName, methodName)>
									<#if privilegeFlags?length gt 0>
										<#assign privilegeIndex = privilegeIndex + 1>
										<img class="clickStyle" src="${imagePathPrefix}/images/permission/flag0.gif"
											flag="${privilegeFlags}" id="${tabId}${modelName?replace('.', '_')}_${methodName}"
											privilege="${execute.getPrivilegeIds(category, modelName, methodName)}" 
											onclick="switchFlag(this, '${tabId}privilege${privilegeIndex}')"><input
											type=hidden value="" name="privileges" id="${tabId}privilege${privilegeIndex}">
									</#if>
								</td>
							</#list>
						</tr>
					</#list>
				</tbody>
			</table>
			
			<#assign othersKeys = execute.getOthersKeys(category)/>
			<#if othersKeys?size gt 0>
			
			<div class="otherPermission">
				<div class="otherPermissionTitle">${action.getText('其它')}</div>
				<div>
					<#list othersKeys as otherKey>
						<span class="otherPermissionCell">
							<#assign privilegeFlags=execute.getOtherPrivilegeFlags(category, otherKey)>
							<#if privilegeFlags?length gt 0>
								<#assign privilegeIndex = privilegeIndex + 1>
								<img class="clickStyle" src="${imagePathPrefix}/images/permission/flag0.gif"
									flag="${privilegeFlags}" id="${tabId}${otherKey?replace('.', '_')}"
									privilege="${execute.getOtherPrivilegeIds(category, otherKey)}" 
									onclick="switchFlag(this, '${tabId}privilege${privilegeIndex}')"/>
								<input type=hidden value="" name="privileges" id="${tabId}privilege${privilegeIndex}"/>
							</#if>
							<label>
								<#assign privilegeName = execute.getOtherPrivilegeI18n(category, otherKey)>
								${privilegeName}
							</label>
						</span>
					</#list>
				</div>
			</div>
			</#if>
		</div>
	</#list>
</div>

<div class="permissionDescription">
	<img src="${imagePathPrefix}/images/permission/flag0.gif" title="None"><span>${action.getText('无')}</span>
	<img src="${imagePathPrefix}/images/permission/flag1.gif" title="Basic"><span>${action.getText('用户级')}</span>
	<img src="${imagePathPrefix}/images/permission/flag2.gif" title="Local"><span>${action.getText('部门级')}</span>
	<img src="${imagePathPrefix}/images/permission/flag3.gif" title="Deep"><span>${action.getText('部门领导级')}</span>
	<img src="${imagePathPrefix}/images/permission/flag4.gif" title="Global"><span>${action.getText('全部')}</span>
</div>
</#if>